package com.rock.code.bigdata.load.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rock.code.bigdata.load.dao.LoadDao;
import com.rock.code.bigdata.load.service.LoadService;
import com.rock.code.common.pager.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by SJ on 2018/7/27.
 */

@Service
public class LoadServiceImpl implements LoadService {

    @Resource
    private LoadDao loadDao;

    @Override
    public String queryTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -0);
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        String day = new SimpleDateFormat("yyyyMMdd").format(time);
        return "tb_kpi_value_day_" + day;
    }

    @Override
    public String queryTbTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        String date = new SimpleDateFormat("yyyyMMdd").format(time);
        return "tb_kpi_value_day_" + date;
    }

    @Override
    public String lastYearlastMonth() {
        //获取去年昨天的时间
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);
        cal.add(Calendar.MONTH, -1);
        Date time = cal.getTime();
        String date = new SimpleDateFormat("yyyyMM").format(time);
        return "tb_kpi_value_month_" + date;
    }

    @Override
    public String weekTableName() {
        Calendar cal = Calendar.getInstance();
        String name = "";
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String lastweek = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1);
        String weeknum = String.valueOf(cal.getActualMaximum(Calendar.WEEK_OF_YEAR));//52
        if (lastweek.equals(weeknum)) {
            cal.add(Calendar.YEAR, -1);
            String lastmonthtable = new SimpleDateFormat("yyyy").format(cal.getTime());
            name = "tb_kpi_value_week_" + lastmonthtable;//2017
        } else {
            name = "tb_kpi_value_week_" + cal.get(Calendar.YEAR);//2018
        }
        return name;
    }

    public String lastMonthLastWeekTableName() {
        Calendar cal = Calendar.getInstance();
        String name = "";
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String lastweek = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1 - 4);//上个月的上周
        String weeknum = String.valueOf(cal.getActualMaximum(Calendar.WEEK_OF_YEAR));//52
        if (lastweek.equals(weeknum)) {
            cal.add(Calendar.YEAR, -1);
            String lastmonthtable = new SimpleDateFormat("yyyy").format(cal.getTime());
            name = "tb_kpi_value_week_" + lastmonthtable;//2017
        } else {
            name = "tb_kpi_value_week_" + cal.get(Calendar.YEAR);//2018
        }
        return name;
    }

    @Override
    public String lastYearWeekTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);
        String lastmonthtable = new SimpleDateFormat("yyyy").format(cal.getTime());
        return "tb_kpi_value_week_" + lastmonthtable;
    }

    @Override
    public String monthTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String month = sdf.format(time);
        return "tb_kpi_value_month_" + month;
    }

    @Override
    public String lastMonthTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        String lastmonthtable = new SimpleDateFormat("yyyyMM").format(cal.getTime());
        return "tb_kpi_value_month_" + lastmonthtable;
    }

    @Override
    public String lastTwoMonthTableName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -2);
        String date = new SimpleDateFormat("yyyyMM").format(cal.getTime());
        return "tb_kpi_value_month_" + date;
    }

    @Override
    public String getLastMonthYesTerday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        cal.add(Calendar.DATE, -1);
        String lastMonthYesTerday = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
        return "tb_kpi_value_day_" + lastMonthYesTerday;
    }

    @Override
    public String yearTableName() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        String year = new SimpleDateFormat("yyyy").format(cal.getTime());
        return "tb_kpi_value_year_" + year;
    }

    @Override
    public String lastYearTableName() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        String year = new SimpleDateFormat("yyyy").format(cal.getTime());
        int lastYear = Integer.parseInt(year) - 1;
        return "tb_kpi_value_year_" + lastYear;
    }

    @Override
    public String lastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        String month = new SimpleDateFormat("yyyy-MM").format(cal.getTime());
        return month;
    }

    @Override
    public String lastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String lastWeek = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1);
        System.out.println("上周是这年的第" + lastWeek + "周");
        return lastWeek;
    }

    @Override
    public String lastWeekInYear() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        String lastWeek = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1 - 4);
        System.out.println("上个月的上周是这年的第" + lastWeek + "周");
        return lastWeek;
    }

    @Override
    public String getYesTerday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }

    /*****************************service***************************************/
    @Override
    public List queryLoad(Map map) {
        return loadDao.queryLoad(map);
    }

    @Override
    public List queryTableSpace(Map map) {
        return loadDao.queryTableSpace(map);
    }


    @Override
    public List queryState(Map map) {
        return loadDao.queryState(map);
    }

    @Override
    public HashMap queryLineByDate(String EXT_UNIT_ID, String KPI_CODE) {
        HashMap resultMap = new HashMap();//结果集
        HashMap map = new HashMap();//装参数
        //*******************上周******************//
        //先确定上周是这年的第几周，再去周表查出这周的天数，再去月表根据日期查出使用率
        Map map1 = new HashMap();
//        map1.put("lastweek", this.lastWeek());
        map1.put("lastweek", "1");
//        map1.put("tableName", this.weekTableName());
        map1.put("tableName", "tb_kpi_value_week_2018");
        map1.put("EXT_UNIT_ID", EXT_UNIT_ID);
        map1.put("KPI_CODE", KPI_CODE);
        List list1 = loadDao.queryDateByWeek(map1);//根据上周第几周查询该周从几号到几号
        //==============================注释掉部分参数写死不需要截========================
//        String str = "";
//        for (Object o : list1) {
//            System.out.println(o.toString());
//            str = o.toString();
//        }
//        String a = str.split(",")[0];
//        String b = str.split(",")[1];
//        String list_start = a.split("=")[1];//开始时间
//        System.out.println(list_start);
//        String d = b.split("=")[1];
//        String list_end = d.replace("}", "");//结束时间
//        System.out.println(list_end);
//        /****表名***/
//        String startTime = list_start.split("-")[0] + list_start.split("-")[1];
//        String tableName_one = "tb_kpi_value_month_" + startTime;
//        String endTime = list_end.split("-")[0] + list_end.split("-")[1];
//        String tableName_two = "tb_kpi_value_month_" + endTime;
//==========================================================================
//        map.put("table_one", tableName_one);
        map.put("table_one", "tb_kpi_value_month_201801");
//        map.put("table_two", tableName_two);
        map.put("table_two", "tb_kpi_value_month_201801");
//        map.put("start_time", list_end);
        map.put("start_time", "2018-01-01");
//        map.put("end_time", list_start);
        map.put("end_time", "2018-01-07");
        map.put("EXT_UNIT_ID", EXT_UNIT_ID);
        map.put("KPI_CODE", KPI_CODE);
        List list = loadDao.queryLineByDate(map);
//         ******************同比********************
//         去年的上周   先去去年周表查出去年的这个上周几号到几号，再去去年这个月的月表查出使用率
        Map map2 = new HashMap();
//        map2.put("lastweek",this.lastWeek());
        map2.put("lastweek", "2");
//        map2.put("tableName",this.lastYearWeekTableName());
        map2.put("tableName", "tb_kpi_value_week_2018");
        map2.put("EXT_UNIT_ID", EXT_UNIT_ID);
        map2.put("KPI_CODE", KPI_CODE);
        List list2 = loadDao.queryDateByWeek(map2);
        //==============================注释掉部分参数写死不需要截========================
//        String str2 = "";
//        for (Object o : list2) {
//            str2 = o.toString();
//        }
//        System.out.println(str);
//        String a2 = str2.split(",")[0];
//        String b2 = str2.split(",")[1];
//        String list2_start = a2.split("=")[1];//开始时间
//        String d2 = b2.split("=")[1];
//        String list2_end = d2.replace("}", "");//结束时间
//
////        ***月表名**
//        String startTime2 = list2_start.split("-")[0] + list2_start.split("-")[1];
//        String tableName_one2 = "tb_kpi_value_month_" + startTime2;
//        String endTime2 = list2_end.split("-")[0] + list2_end.split("-")[1];
//        String tableName_two2 = "tb_kpi_value_month_" + endTime2;
//==========================================================================
//        map.put("table_one",tableName_one2);
        map.put("table_one", "tb_kpi_value_month_201801");
//        map.put("table_two",tableName_two2);
        map.put("table_two", "tb_kpi_value_month_201801");
//        map.put("start_time",list2_end);
        map.put("start_time", "2018-01-08");
//        map.put("end_time",list2_start);
        map.put("end_time", "2018-01-14");
        map.put("EXT_UNIT_ID", EXT_UNIT_ID);
        map.put("KPI_CODE", KPI_CODE);
        List listTb = loadDao.queryLineByDate(map);

//        ***********************环比***********************
//         上上周   先确定上周是这个月的第几周，在确定上个月的这周是几号到几号
        Map map3 = new HashMap();
//        map3.put("lastweek",this.lastWeekInYear());
        map3.put("lastweek", "3");
//        map3.put("tableName",this.lastMonthLastWeekTableName());
        map3.put("tableName", "tb_kpi_value_week_2018");
        map3.put("EXT_UNIT_ID", EXT_UNIT_ID);
        map3.put("KPI_CODE", KPI_CODE);
        List list3 = loadDao.queryDateByWeek(map3);//查询该周从几号到几号

        //==============================注释掉部分参数写死不需要截========================
//        String str3 = "";
//        for (Object o : list3) {
//            str3 = o.toString();
//        }
//        String a3 = str3.split(",")[0];
//        String b3 = str3.split(",")[1];
//        String list3_start = a3.split("=")[1];//开始时间
//        String d3 = b3.split("=")[1];
//        String list3_end = d3.replace("}", "");//结束时间
//
////        **表名*
//        String startTime3 = list3_start.split("-")[0] + list3_start.split("-")[1];
//        String tableName_one3 = "tb_kpi_value_month_" + startTime3;
//        String endTime3 = list3_end.split("-")[0] + list3_end.split("-")[1];
//        String tableName_two3 = "tb_kpi_value_month_" + endTime3;
//==========================================================================
//        map.put("table_one",tableName_one3);
        map.put("table_one", "tb_kpi_value_month_201801");
//        map.put("table_two",tableName_two3);
        map.put("table_two", "tb_kpi_value_month_201801");
//        map.put("start_time",list3_end);
        map.put("start_time", "2018-01-16");
//        map.put("end_time",list3_start);
        map.put("end_time", "2018-01-22");
        map.put("EXT_UNIT_ID", EXT_UNIT_ID);
        map.put("KPI_CODE", KPI_CODE);
        List listHb = loadDao.queryLineByDate(map);

        resultMap.put("list", list);
        resultMap.put("listTb", listTb);
        resultMap.put("listHb", listHb);

        return resultMap;
    }

    @Override
    public PageInfo queryPageList(Page page, Map<String, Object> param) throws Exception {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<Map> list = loadDao.queryListByParaMap(param);
      /*  for (Object o : list) {
            System.out.println(o);
        }*/
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryPageListByYear(Page page, Map<String, Object> param) throws Exception {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<Map> list = loadDao.queryListByParaMapByYear(param);
        return new PageInfo(list);
    }

    @Override
    public List queryLineByDate1(HashMap map) {
        return loadDao.queryLineByDate1(map);
    }

    @Override
    public List queryZztWeek(HashMap map) {
        return loadDao.queryZztWeek(map);
    }

    @Override
    public List queryTable(Map map) {
        return loadDao.queryTable(map);
    }

    @Override
    public List queryInfo(Map map) {
        return loadDao.queryInfo(map);
    }

    @Override
    public List queryDateByWeek(Map map) {
        return loadDao.queryDateByWeek(map);
    }

    @Override
    public String tjKpi(Map map) throws Exception{
        return loadDao.tjKpi(map);
    }
}
